<?php 
/*
**时尚起义安装程序
**install.php
**By QINIAO
*/
define('QINIAO_ROOT',dirname(__file__));
define('DATA', './data/');

require_once(QINIAO_ROOT.'/inc/function.base.php');
require_once(QINIAO_ROOT.'/model/class.mysql.php');

header('Content-Type: text/html; charset=UTF-8');

define('QINIAO_VERSION','V.0.2');

// read_file
function read_file($l1){
return @file_get_contents($l1);
}

$act = isset($_GET['action'])? $_GET['action'] : '';

if(is_file(DATA.'install.lock')){
echo "已经安装过时尚起义微博系统,重新安装请先删除".DATA."install.lock 文件!";	
exit;
}

if(!$act){

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>时尚起义<?php echo QINIAO_VERSION ?>  安装程序</title>
<style type="text/css">
<!--
body {background-color:#F7F7F7;font-family: Arial;font-size: 12px;line-height:150%;}
a{color:#ff6600;}
.main {background-color:#FFFFFF;margin-top:20px;font-size: 12px;color: #666666;width:580px;margin:10px auto;padding:10px;list-style:none;border:#DFDFDF 1px solid;}
#top-title{background:url(images/logo.gif) #9AE3E9 no-repeat right;padding:5px 0px;margin:0px 0px 60px 0px;color:#FFFFFF;}
.cons{margin-left:50px;}
.input {border: 1px solid #CCCCCC;font-family: Arial;font-size: 18px;height:28px;background-color:#F7F7F7;color: #666666;margin:5px 25px 5px 0;}
.submit{background-color:#FFFFFF;border: 3px double #999;border-left-color: #ccc;border-top-color: #ccc;color: #333;padding: 0.25em;cursor:hand;}
.title{font-size:24px;font-weight:bold;}
.care{color:#0066CC;}
.title2{font-size:14px;color:#000000;font-weight:bold;color:#666666;}
.foot{text-align:center;}
-->
</style>
</head>
<body>
<div class="main">
<form name="form1"  method="post" action="install.php?action=install">
<div id="top-title">
<p><span class="title">时尚起义 <?php echo QINIAO_VERSION ?></span><span> 安装程序<br></span></p>
</div>
<div class="cons">
<p class="title2">数据库设置(MySql数据库)</p>
<li>数据库地址：<span class="care">(通常为localhsot)</span><br /><input name="hostname" class="input" value="localhost" /></li>
<li>数据库用户名：<br /><input name="dbuser" class="input" type="text" value=""  /></li>
<li>数据库密码：<br /><input name="password" class="input" type="password" /></li>
<li>数据库名：<span class="care">(程序不会自动创建数据库，请提前创建一个空数据库或使用已有数据库)</span><br /><input name="dbname" class="input" type="text" value=""  /> </li>
<li>数据库前缀：<span class="care">(可随意填写，由英文字母、数字、下划线组成，且必须以下划线结束)</span><input name="dbprefix" class="input" type="text" value="qi_" /></li>
</div>
<div>
<p class="foot">
<input name="Submit" type="submit" class="submit" value="确 定">
<input name="Submit2" type="reset" class="submit" value="重 置">
</p>
</div>

<div><p class="foot">Powered by <a href="http://www.77661.cn/">时尚起义<?php echo QINIAO_VERSION ?></a></p></div>
</form>
</div>
</body>
</html>
<?php 
}
if($act == 'install' || $act =='reinstall')
{
$db_host = $_POST['hostname'];
$db_user = $_POST['dbuser'];
$db_pwd = $_POST['password'];
$db_name = $_POST['dbname'];
$db_prefix = $_POST['dbprefix'];
$result = '';

if(empty($db_prefix))
{
qiMsg('数据库前缀不能为空！');
}elseif(!preg_match("/^[\w_]+_$/",$db_prefix))
{
qiMsg('数据库前缀格式错误！');	
}

//初始化数据库类
$DB = new MySql($db_host,$db_user,$db_pwd,$db_name);
if($act != 'reinstall' && $DB->num_rows($DB->query("SHOW TABLES LIKE '{$db_prefix}mlogs'")) == 1)
{
echo <<<EOT
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>时尚起义系统信息</title>
</head>
<body>
<div>
<form name="form1" method="post" action="install.php?action=reinstall">
<p><input name="hostname" type="hidden" value="$db_host" /></p>
<p><input name="dbuser" type="hidden" value="$db_user" />	</p>
<p><input name="password" type="hidden" value="$db_pwd" /></p>
<p><input name="dbname" type="hidden" value="$db_name" /></p>
<p><input name="dbprefix" type="hidden" value="$db_prefix" /></p>
<p>你的时尚起义V.0.2 微博系统 看起来已经安装过，继续安装可能会覆盖你原有的数据，你要继续安装吗？</p>
<p><input name="submit" type="submit" value="继续>>" /> <a href="javascript:history.back(-1);">&laquo;点击返回</a></p>
</form>
</div>	
</body>
</html>
EOT;
exit;
}

if(!is_writable('config.inc.php'))
{
qiMsg('配置文件(config.inc.php)不可写。如果您使用的是Unix/Linux主机，请修改该文件的权限为777。如果您使用的是Windows主机，请联系管理员，将此文件设为everyone可写');
}
$config = "<?php\n"
."//数据库地址\n"
."define('DB_HOST','$db_host');\n"
."//数据库用户名\n"
."define('DB_USER','$db_user');\n"
."//数据库密码\n"
."define('DB_PASSWD','$db_pwd');\n"
."//数据库名\n"
."define('DB_NAME','$db_name');\n"
."//数据库前缀\n"
."define('DB_PREFIX','$db_prefix');\n"
." ?>";

$fp = @fopen('config.inc.php','w');
$fw = @fwrite($fp,$config);

if(!$fw)
{
qiMsg('配置文件(config.inc.php)不可写。如果您使用的是Unix/Linux主机，请修改该文件的权限为777。如果您使用的是Windows主机，请联系管理员，将此文件设为everyone可写');
}else{
$result.="配置文件修改成功<br />";
}
fclose($fp);

//$setchar = $DB->getMysqlVersion() > '5.0' ? "ALTER DATABASE {$db_name} DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;" : '';

$sql = read_file(DATA.'install.sql');
//$sql = $setchar.$sql;
$sql = str_replace('qi_',$db_prefix,$sql);

$array_sql = preg_split("/;[\r\n]/", $sql);
foreach($array_sql as $sql)
{
$sql = trim($sql);
if ($sql)
{
if (strstr($sql, 'CREATE TABLE'))
{
preg_match('/CREATE TABLE ([^ ]*)/', $sql, $matches);
$ret = $DB->query($sql);
if ($ret)
{
$result .= '数据库表：'.$matches[1].' 创建成功<br />';
}
} else {
$ret = $DB->query($sql);
}
}
}
touch(DATA.'install.lock');	
$result .= "恭喜你！时尚起义".QINIAO_VERSION."安装成功<br /><span style=\"color:red;\">管理员：admin 密码：123456</span><br /><a href=\"./admin/\"> 
进入管理后台>></a> <br /><a href=\"./\"> 
进入网站首页>></a>";
qiMsg($result);

}
?>